package com.example.common;

import com.example.common.enums.Code;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description : 全局通用响应信息主体
 * @Date : 2021/6/17
 * @Author : WangYiFa
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private String msg;

    private T data;

    private Integer code;


    public static final String PAGE_NO = "1";//默认当前页
    public static final String PAGE_SIZE = "10";//默认每页条数

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 接口返回成功
     * @return
     */
    public static <T> CommonResult<T> success() {
        CommonResult result = new CommonResult();
        result.setCode(Code.CODE_S.getIndex());
        result.setMsg(Code.CODE_S.getName());
        result.setData(null);
        return result;
    }

    /**
     * 接口返回成功
     * @param content
     * @return
     */
    public static <T> CommonResult<T> success(T content) {
        CommonResult result = new CommonResult();
        result.setCode(Code.CODE_S.getIndex());
        result.setMsg(Code.CODE_S.getName());
        result.setData(content);
        return result;
    }

    /**
     * 接口返回成功
     * @param data
     * @return
     */
    public static <T> CommonResult<T> success(String data) {
        CommonResult result = new CommonResult();
        result.setCode(Code.CODE_S.getIndex());
        result.setMsg(Code.CODE_S.getName());
        result.setData(data);
        return result;
    }

    /**
     * 接口返回成功
     * @param content
     * @param code
     * @return
     */
    public static <T> CommonResult<T> success(T content , Code code) {
        CommonResult result = new CommonResult();
        result.setCode(code.getIndex());
        result.setMsg(code.getName());
        result.setData(content);
        return result;
    }

    /**
     * 接口返回成功
     * @param content
     * @param code
     * @param message
     * @return
     */
    public static <T> CommonResult<T> success(T content , Code code , String message) {
        CommonResult result = new CommonResult();
        result.setCode(code.getIndex());
        result.setMsg(message);
        result.setData(content);
        return result;
    }

    /**
     * 接口返回异常
     * @return
     */
    public static <T> CommonResult<T> exception() {
        CommonResult result = new CommonResult();
        result.setCode(Code.CODE_E.getIndex());
        result.setMsg("服务器繁忙,请您稍后再试！技术支持电话：16628750949"); //服务器累了,又有人要扣奖金了^-^
        return result;
    }

    /**
     * 接口返回异常
     * @param code
     * @return
     */
    public static <T> CommonResult<T> exception(Code code) {
        CommonResult result = new CommonResult();
        result.setMsg(code.getName());
        result.setCode(code.getIndex());
        return result;
    }

    /**
     * 接口返回错误
     * @param code
     * @return
     */
    public static <T> CommonResult<T> error(Code code) {
        CommonResult result = new CommonResult();
        result.setCode(code.getIndex());
        result.setMsg(code.getName());
        return result;
    }

    /**
     * 接口返回错误
     * @param message
     * @return
     */
    public static <T> CommonResult<T> error(String message) {
        CommonResult result = new CommonResult();
        result.setCode(Code.CODE_F.getIndex());
        result.setMsg(message);
        return result;
    }

    /**
     * 接口返回错误
     * @param message
     * @param code
     * @return
     */
    public static <T> CommonResult<T> error(String message , String code) {
        CommonResult result = new CommonResult();
        result.setCode(Integer.parseInt(code));
        result.setMsg(message);
        return result;
    }

}
